package com.ebocy.hammer.battle.fight.task;

import java.util.Iterator;
import java.util.Map.Entry;

import com.ebocy.hammer.battle.BattleCache;
import com.ebocy.hammer.battle.fight.service.FightService;
import com.ebocy.hammer.core.GameCache;
import com.ebocy.hammer.core.domain.Room;

public class GameEndTask extends Thread {
	FightService fightService = FightService.getInstance();

	@Override
	public void run() {
		try {
			Iterator<Entry<Integer, Long>> it = BattleCache.getGameEndTimer().entrySet().iterator();
			long currentTimeMillis = System.currentTimeMillis();
			while (it.hasNext()) {
				Entry<Integer, Long> entry = it.next();
				if (currentTimeMillis >= entry.getValue()) {
					int roomId = entry.getKey();
					it.remove();
					// 计算战斗结果，并通知房间内玩家
					Room room = GameCache.getRoom(roomId);
					if (room == null)
						continue;
					fightService.calculateBattleResult(room);
				}
			}
		} catch (Exception e) {
			System.out.println("战斗结束定时任务出错###");
			e.printStackTrace();
		}
	}

}
